// package/pages/cardDetail/cardDetail.js
let myRequest = require('../../../common/script/myRequest.js');
let api = require('../../../common/script/api.js');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    hiddenLoading: true,
    memberData: {},
    code: "",
    imgQRCode: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const that = this;
    that.getCardinfoData();
    // this.aginGetdata();
    this.timeOut = null;

  },

  //解绑
  btn() {
    const that = this;
    my.showModal({
      content: '是否解绑会员卡?',
      success(res) {
        if (res.confirm) {
          console.log('用户点击确定')
          let data = {
            appid: my.getAppIdSync().appId,
            cardnumber: that.data.memberData.cardnumber,
            openId: my.getStorageSync({
              key: 'openId'
            }).data,
          }
          my.showLoading({
            title: '加载中...',
          })
          myRequest.EasyHttp.post(api.reqUrls.AlipayMemberCardUnbind, data).then(res => {
            my.hideLoading();
            console.log(res, 9999)
            if (res.data.data.success == true) {
              my.setStorageSync({
                key: 'isVip',
                data: false
              })
              my.setStorageSync({
                key: 'refresh',
                data: false
              })
              my.setStorageSync({
                key: 'isfoundingmember',
                data: false
              })
              my.setStorageSync({
                key: 'cardnumber',
                data: ''
              })
              my.setStorageSync({
                key: 'totalamount',
                data: 0
              })
              my.setStorageSync({
                key: 'userName',
                data: ''
              })
              my.setStorageSync({
                key: 'userPhone',
                data: ''
              })
              my.showModal({
                content: res.data.data.msg,
                showCancel: false,
                success() {
                  my.navigateBack()
                }
              })
            } else {
              console.log(2222)
              my.showModal({
                content: res.data.data.msg,
                showCancel: false,
                confirmText: '我知道了',
                confirmColor: '#ff5151',
                success(e) {}
              })
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })

  },
  getCardinfoData() {
    const that = this;
    let data = {}
    my.showLoading({
      title: '加载中...',
    })
    myRequest.EasyHttp.post(api.reqUrls.membercardShow, data).then(res => {
      my.hideLoading();
      if (res.data.status == 1) {
        that.setData({
          memberData: res.data.result.member_data,
        })
        that.aginGetdata()
      } else {
        my.showModal({
          content: res.data.msg,
          showCancel: false,
          confirmText: '我知道了',
          confirmColor: '#ff5151',
          success(e) {
            my.navigateBack()
          }
        })
      }
    })

  },

  aginGetdata(e) {
    const that = this;
    let data = {}
    myRequest.EasyHttp.post(api.reqUrls.getCardNumberCode, data).then(res => {
      if (res.data.status == 1) {
        that.setData({
          code: res.data.result.card_number_code
        })
        my.getSystemInfo({
          success: (prop) => {
            const viewWidth = parseInt(prop.windowWidth * 0.9);
            my.generateImageFromCode({
              code: res.data.result.card_number_code, // 必填
              format: 'QRCODE', // 或'BARCODE'
              width: viewWidth, // 图片宽度(px)
              success: (res) => {
                this.setData({
                  imgQRCode: res.image
                });
              },
              fail: (err) => {
                console.log(err)
              }
            });


          }
        })
      } else {
        my.hideLoading();
      }
    })
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})